#!/bin/bash
#
# Collect logs after an integration test failure.

# We intentionally don't set '-e' (exit on first failure) since we don't want
# to fail on these diagnostic steps
set -uxo pipefail

LOG_DIR=${LOG_DIR:-/tmp/devstack-logs}
mkdir -p "$LOG_DIR"
# shellcheck disable=SC2024
sudo journalctl -o short-precise --no-pager &> "$LOG_DIR/journal.log"
# shellcheck disable=SC2024
sudo systemctl status "devstack@*" &> "$LOG_DIR/devstack-services.txt"
for service in $(systemctl list-units --output json devstack@* | jq -r '.[].unit | capture("devstack@(?<svc>[a-z\\-]+).service") | '.svc'')
do
    journalctl -u "devstack@${service}.service" --no-tail > "${LOG_DIR}/${service}.log"
done
free -m > "$LOG_DIR/free.txt"
dpkg -l > "$LOG_DIR/dpkg-l.txt"
pip freeze > "$LOG_DIR/pip-freeze.txt"
cp ./devstack/local.conf "$LOG_DIR"
sudo find "$LOG_DIR" -type d -exec chmod 0755 {} \;
sudo find "$LOG_DIR" -type f -exec chmod 0644 {} \;
